﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using carXapp2;

namespace carXapp2.MiscPages
{
    public partial class Page1 : PhoneApplicationPage
    {
        private string carID;
        public Page1()
        {
            InitializeComponent();

            this.DataContext = App.ViewModel;
          
        }

        
        
        private void saveClick(object sender, EventArgs e)
        {
            if (carID == null)
            {
                addCar();
            }
            else
            {
                updateCar();
            }
                if (NavigationService.CanGoBack)
                {
                    NavigationService.GoBack();
                }
            

        }

        private void addCar()
        {
            if (txtMake.Text.Length > 0)
            {
                carInfo newCar = new carInfo
                {
                    CarMake = txtMake.Text,
                    CarModel = txtModel.Text,
                    CarYear = txtYear.Text,
                    CarLic = txtLic.Text,
                    CarInsurance = txtInsurance.Text,
                    CarVin = txtVin.Text,
                    CarInsExpiry = InsExpiry.Value.Value.Date,
                    CarRegExpiry = RegExpiry.Value.Value.Date,

                };
                App.ViewModel.Addcar(newCar);
                App.ViewModel.SingleReminder = new reminderInfo();
                App.ViewModel.SingleReminder.CarID = newCar.CarID;
                App.ViewModel.SingleReminder.Date = InsExpiry.Value.Value;
                App.ViewModel.SingleReminder.DateStr = InsExpiry.Value.Value.ToShortDateString();
                App.ViewModel.SingleReminder.Item = "Insurance";
                App.ViewModel.SingleReminder.Desc = "Insurance Expiry";
                App.ViewModel.carDataDb.reminderInfo.InsertOnSubmit(App.ViewModel.SingleReminder);
                App.ViewModel.SaveChangesToDB();
                App.ViewModel.SingleReminder = new reminderInfo();
                App.ViewModel.SingleReminder.CarID = newCar.CarID;
                App.ViewModel.SingleReminder.Date = RegExpiry.Value.Value;
                App.ViewModel.SingleReminder.DateStr = RegExpiry.Value.Value.ToShortDateString();
                App.ViewModel.SingleReminder.Item = "Registration";
                App.ViewModel.SingleReminder.Desc = "Registration Expiry";
                App.ViewModel.carDataDb.reminderInfo.InsertOnSubmit(App.ViewModel.SingleReminder);
                App.ViewModel.SaveChangesToDB();
            }
        }

        private void updateCar()
        {
            if (txtMake.Text.Length > 0)
            {
                  App.ViewModel.SaveChangesToDB();
 
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            NavigationContext.QueryString.TryGetValue("id", out carID);
            grid1.DataContext = App.ViewModel.SingleCarRecord;
            InsExpiry.DataContext = App.ViewModel.SingleCarRecord;
            RegExpiry.DataContext = App.ViewModel.SingleCarRecord;
            //menuDel.IsEnabled = false;
            base.OnNavigatedTo(e);
        }

        
       

        private void DeleteClick(object sender, EventArgs e)
        {
            if (carID == null)
            {
                MessageBox.Show("Car not saved, cannot delete");
            }
            else
            {
            MessageBoxButton cancel = MessageBoxButton.OKCancel;
            MessageBoxResult result = MessageBox.Show("Are you sure to delete the entire car record", "Confirm",cancel);
            if (result == MessageBoxResult.OK)
            {
                //carInfo DeleteCar = App.ViewModel.Cars[int.Parse(carID)] as carInfo;
                App.ViewModel.DeleteCar(App.ViewModel.SingleCarRecord);
                App.ViewModel.DeleteAllFuel(int.Parse(carID));
                App.ViewModel.DeleteAllMaint(int.Parse(carID));
                App.ViewModel.DeleteAllReminder(int.Parse(carID));
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                
            }
            }
        }


    }
}